package com.txy.webmanagement.service;

import com.gitee.sunchenbin.mybatis.actable.command.PageResultCommand;
import com.gitee.sunchenbin.mybatis.actable.manager.common.BaseMysqlCRUDManager;
import com.txy.webmanagement.dto.SysCompanyDTO;
import com.txy.webmanagement.mapper.SysCompanyMapper;
import com.txy.webmanagement.model.SysCompany;
import com.txy.webmanagement.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author wf
 * @date 2019-03-22$
 **/
@Service
public class SysCompanyService {
    @Autowired
    private BaseMysqlCRUDManager baseMysqlCRUDManager;

    public List<SysCompany> pageList(SysCompanyDTO dto) {
        SysCompany c = new SysCompany();
        c.setCompanyName(dto.getCompanyName());
        c.setPageSize(dto.getPageSize());
        c.setCurrentPage(dto.getCurrentPage());
        PageResultCommand<SysCompany> ss = baseMysqlCRUDManager.query(c);
        List<SysCompany> li = ss.getData();
        return li;
    }

    public Boolean add(SysCompanyDTO dto) {
        SysCompany company = new SysCompany();
        if (StringUtils.checkStringNotNull(dto.getCompanyName())) {
            company.setUuid(StringUtils.getUUID());
            company.setCompanyAddress(dto.getCompanyAddress());
            company.setCompanyName(dto.getCompanyName());
            company.setCorporation(dto.getCorporation());
            company.setPhonyNumber(dto.getPhonyNumber());
            company.setCreateTime(new Date());
            company.setEnable(1);
            Integer sq = baseMysqlCRUDManager.save(company);
            if (sq > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }

    public Boolean update(SysCompanyDTO dto) {
        SysCompany company = getOne(dto.getUuid());
        if (company != null) {
            company.setUuid(StringUtils.getUUID());
            company.setCompanyAddress(dto.getCompanyAddress());
            company.setCompanyName(dto.getCompanyName());
            company.setCorporation(dto.getCorporation());
            company.setPhonyNumber(dto.getPhonyNumber());
            Integer sq = baseMysqlCRUDManager.save(company);
            if (sq > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 获取详情
     *
     * @param companyId
     * @return
     */
    public SysCompany getOne(String companyId) {
        if (StringUtils.checkStringNotNull(companyId)) {
            SysCompany company = new SysCompany();
            company.setUuid(companyId);
            PageResultCommand<SysCompany> ss = baseMysqlCRUDManager.query(company);
            if (ss.getData() != null && ss.getData().size() > 0) {
                return ss.getData().get(0);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    /**
     * 删除
     *
     * @param dto
     * @return
     */
    public Boolean delete(SysCompanyDTO dto) {
        SysCompany com = getOne(dto.getUuid());
        if (com != null) {
            try {
                baseMysqlCRUDManager.delete(com);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return false;
        }
    }
}
